import os

def run_cmd(cmd):
    print(cmd)
    os.system(cmd)

def create_dirs(dirs):
    for dir in dirs:
        if not os.path.exists(dir):
            os.makedirs(dir)
    
# input directory and output directory.
project_name = 'fastcsv'
srcDir = "./src"             # source code
outDir = "./build/classes"   # output classes
jarDir = './jars'            # jar files
srcs = [project_name + item for item in ["", ".utils", ".tests"]]

create_dirs([outDir, jarDir])
 
# Format inputs. For example, fastcsv.utils -> ./src/fastcsv/utils/*.java
for i in range(len(srcs)):
    srcs[i] = os.path.join(os.path.join(srcDir, srcs[i].replace('.', '/'), '*.java')).replace('\\', '/')


# create the compiling command
run_cmd("javac -encoding utf-8 -d {0} -cp {1} {2}".format(outDir, srcDir, ' '.join(srcs)))


# make jar file

jarfile = '{0}/{1}.jar'.format(jarDir, project_name)
print('jarfile:', jarfile)
if os.path.exists(jarfile):
    os.remove(jarfile)

run_cmd("jar cvf {0} -C ./build/classes .".format(jarfile))
print("Compiled.")

run_cmd('java -cp ./jars/fastcsv.jar fastcsv.StopWatch')